/**
 * 操作日志查询
 * @authors supmain (mtingfeng@gmail.com)
 * @date    2015-01-29 10:40:15
 * @update  2015-01-29 10:40:15
 * @for webapp.html
 * @version $Id$
 */
var BSTMG = BSTMG || {};
BSTMG.operatorManager = {
	init: function(){
		var that = this;
		/*操作日志列表*/
		that.operateLogListReq("","","");
		/* 查询 */
		that.dateKeyWordQuery();
		/* 日志管理 */
		that.exportLog();
	},
	operateLogListReq: function(starttime,endtime,username){
		var url = "/sysAdmin/operateLogList/search?starttime="+starttime+"&endtime="+endtime+"&username="+username;
		BSTMG.common.hjhPages(url,function(data){
			var operateLogList = data.operateLogList;
			var $tbody = $("#logtable tbody");
			var $tr = '';
			
			if(operateLogList.length > 0) {
				for(var i=0;i<operateLogList.length;i++){
					var userInfo = operateLogList[i];
					$tr += '<tr class="odd">';
					//$tr += '<td><input type="checkbox" class="simple" data-id="'+userInfo.operateLogId+'"></td>';
					$tr += '<td data-id="'+userInfo.operateLogId+'">'+(i+1)+'</td>';
					$tr += '<td>'+ BSTMG.tools.checkNull(toDate(userInfo.operateTime,"yyyy-MM-dd hh:mm:ss"))+'</td>';
					$tr += '<td>'+ BSTMG.tools.checkNull(userInfo.operateName)+'</td>';
					$tr += '<td>'+ BSTMG.tools.checkNull(userInfo.loginIp)+'</td>';
					$tr += '<td>'+ BSTMG.tools.checkNull(userInfo.operateContent)+'</td>';
					$tr += '<td>'+ "成功"+'</td>';
					$tr += '</tr>';
				}
				$tbody.html($tr);
			}else {
				var $tr = '<tr class="odd"><td colspan="6">暂无数据！</td></tr>';
				$tbody.html($tr);
			}
		});
	},
	dateKeyWordQuery: function(){
		var $starttime = $("#starttime"),
			$endtime = $("#endtime"),
			$username = $("#username"),
			$operate_status = $("#operate_status");
		var $query = $("#query"),
			$logtable = $("#logtable");
		/* 获取数据 */
		function getData(){
			return {
				starttime: $.trim( $starttime.val() ),
				endtime: $.trim( $endtime.val() ),
				username: $.trim( $username.val() ),
				status: $.trim( $operate_status.val() )
			};
		};
		/* 校验数据 */
		function checkData( data ){
			if( data.starttime != "" && data.endtime != "" ){
				try{
					var start = ( new Date( data.starttime ) ).getTime(),
						end = ( new Date( data.endtime ) ).getTime();
					if( start > end ){
						BSTYUN.dialog.warn("开始时间不能大于结束时间！");
						return false;
					};
				}catch(e){
					BSTYUN.dialog.warn("日期数据不合法！请重新输入");
					return false;
				}
			};
			return true;
		};
		$query.on("click",function(){
			var data = getData();
			BSTMG.operatorManager.operateLogListReq(data.starttime,data.endtime,data.username);
		});
	},
	exportLog: function(){
		var $operator = $("#log_operator");
		$operator.on("click",".exportlog",function(){
			showOperatorDialog();
		});
		function showOperatorDialog(){
			var dialog = null,
				html = '<div id="addnewcategory">' +
					   '<form class="form-inline" role="form">' +
					   '<div class="form-group" style="margin-right: 20px;">' +
					   '<label for="dianxingdaima">开始时间：</label>' +
					   '<input type="text" id="export_starttime" class="form-control" placeholder="开始时间" onclick="WdatePicker()"></div>' +
					   '<div class="form-group">' +
					   '<label for="dianxingmingcheng">结束时间：</label>' + 
					   '<input type="text" id="export_endtime" class="form-control" placeholder="结束时间" onclick="WdatePicker()"></div>' +
					   '</form></div>';
//					   <div class="form-group" style="display:block;margin-top:20px"><label for="dianxingmingcheng">是否加密：</label><select class="form-control input-sm" name="type" id="encrypt"><option value="1">是</option><option value="0">否</option></select></div>
			dialog = BSTYUN.dialog.creat({
				id : "exportlog",
				title: "导出日志",
				content : html
			});
			dialog.button(
				{
					name : "确定",
					callback : confirmExport
				},{
					name : "取消"
				}
			);
			function confirmExport(){
				var $content = this.DOM.content,
					$starttime = $content.find("#export_starttime"),
					$endtime = $content.find("#export_endtime");
//					$encrypt = $content.find("#encrypt");
				var starttime = $.trim( $starttime.val() ),
					endtime = $.trim( $endtime.val() );
//					encrypt = $.trim( $encrypt.val() );
				if( starttime != "" && endtime != "" ){
					try{
						var start = ( new Date( starttime ) ).getTime(),
							end = ( new Date( endtime ) ).getTime();
						if( start > end ){
							BSTYUN.dialog.warn("开始时间不能大于结束时间！");
							return false;
						};
					}catch(e){
						BSTYUN.dialog.warn("日期数据不合法！请重新输入");
						return false;
					}
				};
				
				/*关闭弹出框*/
				dialog.close();
				/*导出excel文件*/
				location.href = "/sysAdmin/logReport?startTime="+starttime+"&endTime="+endtime;
				return false;
			};
		};
	}
};
$(function(){
	BSTMG.operatorManager.init();
});